<?php
namespace Admin\Controller;
use Think\Controller;

class MemberController extends HomeController {
	/*
	* Function:		memberList
	* Description:	用户列表
	*/
    public function memberList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter = returnFilter($filter);
		$memberList = D('Member')->getMemberList($page,$filter,$pageSize);
		$count =  D('Member')->getMemberCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('memberList',$memberList);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }
	
	/*
	* Function:		addMember
	* Description:	增加用户
	*/
	public function addMember(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			$data['login_pass'] = md5($data['login_pass']);
			$data['addtime'] = date('Y-m-d H:i:s',time());
			if(D('Member')->getMemberByUserName($data['login_name'])){
				$ret = array('code'=>0,'message'=>'用户名不能重复');
				$this->ajaxReturn($ret);
			}
			if($user_id = D('Member')->addMember($data)){
				if(!empty($data['role'])){
					if(!M('MemberRoleUser')->where(array('role_id'=>$data['role'],'user_id'=>$user_id))->find()){
					    M('MemberRoleUser')->where(['user_id'=>$user_id])->delete();
						$rData = array();
						$rData['role_id'] = $data['role'];
						$rData['user_id'] = $user_id;
						M('MemberRoleUser')->add($rData);
					}
				}
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加会员失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$roleList = M('MemberRole')->where(array('status'=>1))->select();
			$this->assign('roleList',$roleList);
			$this->display();
		}
	}
	
	/*
	* Function:		updateMember
	* Description:	更新用户
	*/
	public function updateMember($id = ''){
		if(IS_POST){
			$data = I('post.');
			if($data['login_pass']==''){
				unset($data['login_pass']);
			}else{
				$data['login_pass'] = md5($data['login_pass']);
			}
			if(D('Member')->getMemberByUserName($data['login_name'],$data['id'])){
				$ret = array('code'=>0,'message'=>'用户名不能重复');
				$this->ajaxReturn($ret);
			}
			$ret = D('Member')->updateMember($data);
			if($ret!==false){
				if(!empty($data['role'])){
					if(!M('MemberRoleUser')->where(array('role_id'=>$data['role'],'user_id'=>$data['id']))->find()){
					    M('MemberRoleUser')->where(['user_id'=>$data['id']])->delete();
						$rData = array();
						$rData['role_id'] = $data['role'];
						$rData['user_id'] = $data['id'];
						M('MemberRoleUser')->add($rData);
					}
				}
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新会员失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$memberInfo = D('Member')->getMemberInfo($id);
				$roleList = M('MemberRole')->where(array('status'=>1))->select();
				$this->assign('roleList',$roleList);
				$this->assign('memberInfo',$memberInfo);
				$this->display();
			}
		}
	}
	
	/*
	* 会员注销
	*/
	public function loginOut(){
		session(null);
		$this->success('退出成功！');
	}
	
	/*.
	* 会员修改密码
	*/
	public function chanagePasswd(){
		if(IS_POST){
			$arr = array();
			$data = I('post.');
			$data['login_pass'] = md5($data['login_pass']);
			$data['id'] = session(C('USER_AUTH_KEY'));
			if(!D('Member')->checkPasswdIsExist($data['login_pass'],$data['id'])){
				$ret = array('code'=>0,'message'=>'原始密码不存在!');
				$this->ajaxReturn($ret);
			}
			if($data['newPasswd']!==$data['repeatNewPasswd']){
				$ret = array('code'=>0,'message'=>'两次密码不一致!');
				$this->ajaxReturn($ret);
			}
			$arr['id'] = $data['id'];
			$arr['login_pass'] = md5($data['newPasswd']);
			if(D('Member')->updateMember($arr)){
				session(null);
				$ret = array('code'=>1,'message'=>'修改密码成功!');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'修改密码失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$this->display();
		}
	}
	
	/*
	* Function:		memberRoleList
	* Description:	角色列表
	*/
    public function memberRoleList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter = returnFilter($filter);
		$list = D('MemberRole')->getMemberRoleList($page,$filter,$pageSize);
		$count =  D('MemberRole')->getMemberRoleCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }
	
	/*
	* Function:		addMemberRole
	* Description:	增加角色
	*/
	public function addMemberRole(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			//判断是否已存在-name
			if(M('MemberRole')->where(array('name'=>$data['name']))->find()){
				$ret = array('code'=>0,'message'=>'此角色已存在');
				$this->ajaxReturn($ret);
			}
			if(D('MemberRole')->addMemberRole($data)){
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$this->display();
		}
	}
	
	/*
	* Function:		editMemberRole
	* Description:	更新角色
	*/
	public function editMemberRole($id = ''){
		if(IS_POST){
			$data = I('post.');
			if(M('MemberRole')->where(array('name'=>$data['name'],'id'=>array('neq',$data['id'])))->find()){
				$ret = array('code'=>0,'message'=>'此角色已存在');
				$this->ajaxReturn($ret);
			}
			if(D('MemberRole')->updateMemberRole($data)){
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$info = D('MemberRole')->getMemberRoleInfo($id);
				$this->assign('info',$info);
				$this->display();
			}
		}
	}
	
	/*
	* Function:		authMemberRole
	* Description:	角色授权
	*/
	public function authMemberRole($id){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			//判断是否已存在-name
			$role_id = $data['id'];//角色id
			$addData = array();
			if(is_array($data['node'])&&count($data['node'])>0){
				M('MemberAccess')->where(array('role_id'=>$role_id))->delete();
				foreach($data['node'] as $key=>$val){
					$tmp = explode('|', $val);
					$addData[] = array(
                        'role_id' => $role_id,
                        'node_id' => $tmp[0],
                        'level' => $tmp[1],
						'pid' => $tmp[2]
                    );
				}
				$addData[] = array( //加入顶级节点
					'role_id' => $role_id,
					'node_id' => 1,
					'level' => 1,
					'pid' => 0
				);
				M('MemberAccess')->addAll($addData);
			}
			$ret = array('code'=>1,'message'=>'授权成功');
			$this->ajaxReturn($ret);
		}else{
			$roleNodeList = M('MemberAccess')->where(array('role_id'=>$id))->field('node_id')->select();
			$nodeList = M('MemberNode')->where(array('level'=>2,'status'=>1))->select();
			if(is_array($nodeList)&&count($nodeList)>0){
				foreach($nodeList as $key=>$val){
					$nodeList[$key]['selected'] = 0;
					if(is_array($roleNodeList)&&count($roleNodeList)>0){
						if(array_multi_search($val['id'],$roleNodeList)){
							$nodeList[$key]['selected'] = 1;
						}
					}
					$snodeList = M('MemberNode')->where(array('level'=>3,'status'=>1,'pid'=>$val['id']))->select();
					if(is_array($snodeList)&&count($snodeList)>0){
						foreach($snodeList as $k=>$v){
							$snodeList[$k]['selected'] = 0;
							if(is_array($roleNodeList)&&count($roleNodeList)>0){
								if(array_multi_search($v['id'],$roleNodeList)){
									$snodeList[$k]['selected'] = 1;
								}
							}
						}
					}
					$nodeList[$key]['snodeList'] = $snodeList;
				}
			}
			$this->assign('nodeList',$nodeList);
			$this->assign('id',$id);
			$this->display();
		}
	}
	
	/*
	* Function:		setMemberRole
	* Description:	开启会员角色
	* @param:		$id int id
	*/
	public function setMemberRole($id = '',$status = 1){
		if($info = D('MemberRole')->getMemberRoleInfo($id)){
			if(D('MemberRole')->updateMemberRole(array('id'=>$id,'status'=>$status))){
				$ret = array('code'=>1,'message'=>'操作成功!');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'操作失败');
				$this->ajaxReturn($ret);
			}
		}else{
			$ret = array('code'=>0,'message'=>'不存在此角色!');
			$this->ajaxReturn($ret);
		}
	}
	
	/*
	* Function:		memberNodeList
	* Description:	节点列表
	*/
    public function memberNodeList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter['level'] = 1;
		$filter = returnFilter($filter);
		$list = D('MemberNode')->getMemberNodeList($page,$filter,$pageSize);
		if(is_array($list)&&count($list)>0){
			foreach($list as $key=>$val){
				$slist = M('MemberNode')->where(array('level'=>2,'pid'=>$val['id']))->select();
				if(is_array($slist)&&count($slist)>0){
					foreach($slist as $k=>$v){
						$sslist = M('MemberNode')->where(array('level'=>3,'pid'=>$v['id']))->select();
						$slist[$k]['sslist'] = $sslist;
					}
				}
				$list[$key]['slist'] = $slist;
			}
		}
		$count =  D('MemberNode')->getMemberNodeCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }
	
	/*
	* Function:		addMemberNode
	* Description:	增加功能节点
	*/
	public function addMemberNode(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			//判断是否已存在-name
			// if(M('MemberNode')->where(array('name'=>$data['name']))->find()){
				// $ret = array('code'=>0,'message'=>'此节点已存在');
				// $this->ajaxReturn($ret);
			// }
			if(!empty($data['pid'])){
				$pinfo = M('MemberNode')->where(array('id'=>$data['pid']))->find();
				if($pinfo['level']==1){
					$data['level'] = 2;
				}else if($pinfo['level']==2){
					$data['level'] = 3;
				}
			}else{
				$data['pid'] = 0;
				$data['level'] = 1;
			}
			if(D('MemberNode')->addMemberNode($data)){
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$pidList = M('MemberNode')->where(array('level'=>1))->select();
			if(is_array($pidList)&&count($pidList)>0){
				foreach($pidList as $key=>$val){
					$spidList = M('MemberNode')->where(array('level'=>2,'pid'=>$val['id']))->select();
					$pidList[$key]['spidList'] = $spidList;
				}
			}
			$this->assign('pidList',$pidList);
			$this->display();
		}
	}
	
	/*
	* Function:		editMemberNode
	* Description:	更新功能节点
	*/
	public function editMemberNode($id = ''){
		if(IS_POST){
			$data = I('post.');
			// if(M('MemberNode')->where(array('name'=>$data['name'],'id'=>array('neq',$data['id'])))->find()){
				// $ret = array('code'=>0,'message'=>'此节点已存在');
				// $this->ajaxReturn($ret);
			// }
			if(!empty($data['pid'])){
				$pinfo = M('MemberNode')->where(array('id'=>$data['pid']))->find();
				if($pinfo['level']==1){
					$data['level'] = 2;
				}else if($pinfo['level']==2){
					$data['level'] = 3;
				}
			}else{
				$data['pid'] = 0;
				$data['level'] = 1;
			}
			if(D('MemberNode')->updateMemberNode($data)){
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$info = D('MemberNode')->getMemberNodeInfo($id);
				$this->assign('info',$info);
				$pidList = M('MemberNode')->where(array('level'=>1))->select();
				if(is_array($pidList)&&count($pidList)>0){
					foreach($pidList as $key=>$val){
						$spidList = M('MemberNode')->where(array('level'=>2,'pid'=>$val['id']))->select();
						$pidList[$key]['spidList'] = $spidList;
					}
				}
				$this->assign('pidList',$pidList);
				$this->display();
			}
		}
	}
	
	/*
	* Function:		setMemberNode
	* Description:	开启权限节点
	* @param:		$id int id
	*/
	public function setMemberNode($id = '',$status = 1){
		if($info = D('MemberNode')->getMemberNodeInfo($id)){
			if(D('MemberNode')->updateMemberNode(array('id'=>$id,'status'=>$status))){
				$ret = array('code'=>1,'message'=>'操作成功!');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'操作失败');
				$this->ajaxReturn($ret);
			}
		}else{
			$ret = array('code'=>0,'message'=>'不存在此功能节点!');
			$this->ajaxReturn($ret);
		}
	}
	
	/*
	* Function:		userList
	* Description:	客户账户列表
	*/
    public function userList(){
		$filter = I('param.');
		$page =  I('get.p',1);
		$pageSize = 10;
		$filter['is_kh'] = 1;
		$filter = returnFilter($filter);
		$list = D('Member')->getMemberList($page,$filter,$pageSize);
		if(is_array($list)&&count($list)>0){
			foreach($list as $key=>$val){
				if($cInfo = M('Company')->where(array('user_id'=>$val['id']))->find()){
					$list[$key]['real_name'] = $cInfo['name'];
				}
			}
		}
		$count =  D('Member')->getMemberCount($filter);
		$pages = getpage($count,$pageSize,$filter);
		$show = $pages->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('filter',$filter);
		$this->display();
    }
	
	/*
	* Function:		addUser
	* Description:	增加客户账户
	*/
	public function addUser(){
		$ret = array();
		if(IS_POST){
			$data = I('post.');
			$data['login_pass'] = md5($data['login_pass']);
			$data['addtime'] = date('Y-m-d H:i:s',time());
			if(D('Member')->getMemberByUserName($data['login_name'])){
				$ret = array('code'=>0,'message'=>'用户名不能重复');
				$this->ajaxReturn($ret);
			}
			if(M('Operators')->where(array('telephone'=>$data['telephone']))->find()){
				$ret = array('code'=>0,'message'=>'手机号不能重复');
				$this->ajaxReturn($ret);
			}
			if($user_id = D('Member')->addMember($data)){
				$ret = array('code'=>1,'message'=>'添加成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'添加会员失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			$this->display();
		}
	}
	
	/*
	* Function:		updateUser
	* Description:	更新客户账户
	*/
	public function updateUser($id = ''){
		if(IS_POST){
			$data = I('post.');
			if($data['login_pass']==''){
				unset($data['login_pass']);
			}else{
				$data['login_pass'] = md5($data['login_pass']);
			}
			if(D('Member')->getMemberByUserName($data['login_name'],$data['id'])){
				$ret = array('code'=>0,'message'=>'用户名不能重复');
				$this->ajaxReturn($ret);
			}
			if(M('Operators')->where(array('telephone'=>$data['telephone'],'id'=>array('neq',$data['id'])))->find()){
				$ret = array('code'=>0,'message'=>'手机号不能重复');
				$this->ajaxReturn($ret);
			}
			if(D('Member')->updateMember($data)){
				$ret = array('code'=>1,'message'=>'更新成功');
				$this->ajaxReturn($ret);
			}else{
				$ret = array('code'=>0,'message'=>'更新会员失败!');
				$this->ajaxReturn($ret);
			}
		}else{
			if(empty($id)){
				$this->error('参数异常');
			}else{
				$memberInfo = D('Member')->getMemberInfo($id);
				$this->assign('memberInfo',$memberInfo);
				$this->display();
			}
		}
	}
}